package com.yxhl.km.module.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具
 * Created by yxr on 17/7/17.
 */
public class DateUtil {
    private static final long ONE_MINUTE = 60;
    private static final long ONE_HOUR = 3600;
    private static final long ONE_DAY = 86400;
    private static final long TWO_DAY=ONE_DAY*2;
    private static final long ONE_MONTH = 2592000;
    private static final long ONE_YEAR = 31104000;

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {
        if(null!=str && str.length()>0){
//            if(str.contains(".")){
//                String[] sourceStrArray = str.split("\\.");
//                str=sourceStrArray[0];
//            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = format.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
        return null;
    }

    /**
     * 将短时间格式时间转换为字符串 MM-dd
     *
     */
    public static String dataFormat(Date dateDate, String type) {
        if(null!=dateDate) {
            SimpleDateFormat formatter = new SimpleDateFormat(type);
            String dateString = formatter.format(dateDate);
            return dateString;
        }
        return "";
    }

    /**
     * 距离截止日期还有多长时间
     *
     * @param
     * @return
     */
    public static String fromDeadline(Date endTime, Date startTime) {
        long deadline = startTime.getTime() / 1000;
        long now = (endTime.getTime()) / 1000;
        long remain = deadline - now;
        if (remain <= ONE_HOUR)
            return "00:" + getDigit(remain / ONE_MINUTE) ;
        else if (remain <= TWO_DAY)
            return  getDigit(remain / ONE_HOUR )+ ":"+ getDigit((remain % ONE_HOUR / ONE_MINUTE));
        else {
            return "48+";
        }

    }

    private static String getDigit(long l) {
        if(l<10){
            return "0"+l;
        }
        return l+"";
    }

    public static String getDatePoor(String endDate, String startDate) {

        return getDatePoor(StrToDate( endDate),StrToDate( startDate));
    }

    public static String getDatePoor(Date endDate, Date startDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        if(day>0){
            return day + "天" + hour + "小时" + min + "分";
        }else {
            return  hour + "小时" + min + "分";
        }

    }


}
